﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GameSchool.Models.Interfaces;
using GameSchool.Models;
using System.IO;


namespace GameSchool.Models.Repositories
{
    
    public class CourseRepository 
    {
        GameSchoolDBDataContext db = new GameSchoolDBDataContext();

        //Fetch all users in a course by the CourseID
        public IQueryable<UserProfile> GetUsersByCourse(int? cID)
        {

            var courseUserList = from users in db.UserProfiles
                                 from course in db.IsInCourses
                                 where users.UserID == course.UserID
                                 where course.CourseID == cID
                                 select users;
            return courseUserList;

        }

        //Fetch all courses that exist in the system
        public IEnumerable<KeyValuePair<int, string>> GetCourses()
        {
            var courseList = (from c in db.Courses
                              orderby c.Name ascending
                              select new KeyValuePair<int, string>(c.ID, c.Name) );

            return courseList;
        }
        public Course GetCourseByID(int? cID)
        {
            var courses = (from v in db.Courses
                           where cID == v.ID
                           select v).SingleOrDefault();
            return courses;
        }

        //Updates a course by it's ID
        public void UpdateCourse(Course CourseItem)
        {
            Course oldItem = db.Courses.Where(x => x.ID == CourseItem.ID).SingleOrDefault();
            if (oldItem != null)
            {
                oldItem.Name = CourseItem.Name;
                oldItem.Descr = CourseItem.Descr;

            }
        }

        //Fetch all users in a particular course
        public IEnumerable<KeyValuePair<int, string>> GetCoursesByUserID(Guid uID) 
        {
            IEnumerable<KeyValuePair<int, string>> courseList = (from t in db.IsInCourses
                                                                 where t.UserID == uID
                                                                 from c in db.Courses
                                                                 where c.ID == t.CourseID
                                                                 select new KeyValuePair<int, string>(c.ID, c.Name));
                       
            return courseList;        
        
        }

        //Counts levels in a course, helps with the level view and is a helper function
        public int LevelCount(int? cID)
        {
            int result = (from s in db.Levels
                          where s.CourseID == cID
                          select s).Count();
            return result;

        }

        //Fetches a level by it's course and number
        public Level LevelID(int? cID, int? lvlNR)
        {
            var result = (from s in db.Levels
                          where s.CourseID == cID
                          where s.LevelNr == lvlNR
                          select s).SingleOrDefault();
            return result;
        }
      
        public void Save ()
        {
            db.SubmitChanges();
        }
        
        public void AddCourse(Course item)
        {
            db.Courses.InsertOnSubmit(item);
        }

        public void AddUserToCourse(IsInCourse item)
        {
            db.IsInCourses.InsertOnSubmit(item);
        }

    }

}